//
//  YUpdateUtils.swift
//  NewNGPlay
//
//  Created by YMac on 2017/12/13.
//  Copyright © 2017年 zchd. All rights reserved.
//
open class YUpdateUtils{
    open class func checkUpdate(vc:YUIViewController,httpMethod:String = "POST"){
        YUpdateUtils.checkUpdate(vc: vc, httpMethod:httpMethod, onCheck: { (result,versionEntity) in
            if result {
                vc.yToast("已是最新版本")
            }
        }, mustUpdate: true, showProgress: true,showErrorToast: true)
    }
    
    //onCheck参数true为检测成功，并且为最新版本，false为检测失败，检测成功并且不是最新版自动跳转
    open class func checkUpdate(vc:YUIViewController,httpMethod:String = "POST",onCheck:@escaping (Bool,YVersionModel?) -> (),mustUpdate:Bool,showProgress:Bool,showErrorToast:Bool){
        if TextUtils.isEmpty(YConfig.UPDATEIP) || TextUtils.isEmpty(YConfig.checkUpdateInterface){
            vc.yPrint("版本校验工具参数不完整，请配置YConfig")
        } else {
            vc.yPost(ip:YConfig.UPDATEIP,jkid: YConfig.checkUpdateInterface, kv: nil, httpMethod:httpMethod , callBack: { (isSuccess,results:[YVersionModel], _) in
                if isSuccess {
                    let versionEntity = results[0]
                    if versionEntity.ios_version != YAppUtils.getAppVersionCode() {
                        update(vc: vc, onCheck: onCheck, versionEntity: versionEntity, mustUpdate: mustUpdate)
                    } else {
                        onCheck(true,versionEntity)
                    }
                }else{
                    onCheck(false,nil)
                }
            }, showProgress: showProgress, showErrorToast: showErrorToast)
        }
    }
    
    private class func update(vc:YUIViewController,onCheck:@escaping (Bool,YVersionModel?) -> (),versionEntity:YVersionModel,mustUpdate:Bool){
        let alertView = UIAlertController(title: "\(YAppUtils.getAppName())提示", message: "检测到新版本", preferredStyle: .alert)
        alertView.addAction(
            UIAlertAction(title: "升级", style: .default, handler: { (_) in
                doUpdate(vc: vc, versionEntity: versionEntity)
            })
        )
        if !mustUpdate {
            alertView.addAction(
                UIAlertAction(title: "取消", style: .cancel, handler: { (_) in
                    onCheck(true,versionEntity)
                })
            )
        }
        alertView.view.tintColor = YConfig.themeColor
        vc.present(alertView, animated: true, completion: nil)
    }
    
    open class func doUpdate(vc:YUIViewController,versionEntity:YVersionModel){
        if !TextUtils.isEmpty(versionEntity.appleStore_url){
            UIApplication.shared.openURL(URL(string: versionEntity.appleStore_url!)!)
        }else if(!TextUtils.isEmpty(versionEntity.applePlist_url)){
            // UIApplication.shared.openURL(URL(string: "itms-services://?action=download-manifest&url=\(versionEntity.applePlist_url!)")!)
            var tempStr = "itm"
            tempStr += "s-ser"
            tempStr += "vices:/"
            tempStr += "/?acti"
            tempStr += "on=dow"
            tempStr += "nload-m"
            tempStr += "anifest&url="
            tempStr += versionEntity.applePlist_url!
            UIApplication.shared.openURL(URL(string: tempStr)!)
        }else{
            UIApplication.shared.openURL(URL(string: versionEntity.ios_url)!)
        }
        exit(0)
    }
}
